﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;
using System.Linq.Expressions;

namespace Warehouse.Web.Purchase
{
    public partial class BillList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var allSuppliers = DataAccess.Supplier.GetSuppliers().ToList();
                allSuppliers.Insert(0, new DataAccess.Supplier { ID = -1, Name = "全部" });
                dpSupplier.DataSource = allSuppliers;
                dpSupplier.DataBind();

                var billTypes = new List<ListItem>() { };
                billTypes.Add(new ListItem { Text = "全部", Value = "-1" });
                billTypes.Add(new ListItem { Text = "进货单", Value = ((int)Warehouse.DataAccess.PurchaseBillType.Purchase).ToString() });
                billTypes.Add(new ListItem { Text = "赠品单", Value = ((int)Warehouse.DataAccess.PurchaseBillType.Gift).ToString() });
                billTypes.Add(new ListItem { Text = "退货单", Value = ((int)Warehouse.DataAccess.PurchaseBillType.Return).ToString() });
                billTypes.Add(new ListItem { Text = "付款单", Value = "4" });
                billTypes.Add(new ListItem { Text = "收款单", Value = "5" });
                dpBillType.DataSource = billTypes;
                dpBillType.DataBind();                

                if (Request.QueryString.AllKeys.Contains("search"))
                {
                    DateTime dtStart = DateTime.Parse(Request.QueryString["Start"]);
                    DateTime dtEnd = DateTime.Parse(Request.QueryString["End"]);
                    var supplier = Request.QueryString["Supplier"];
                    var serialNum = Request.QueryString["SerialNum"];
                    var summary = Request.QueryString["Summary"];
                    var type = Request.QueryString["Type"];

                    this.tbStartDate.Text = dtStart.ToString("yyyy-MM-dd");
                    this.tbEndDate.Text = dtEnd.ToString("yyyy-MM-dd");
                    dpSupplier.SelectedValue = supplier;
                    dpBillType.SelectedValue = type;
                    tbSummary.Text = summary;
                    tbSerialNum.Text = serialNum;

                    var suppliers = allSuppliers.ToDictionary(p => p.ID);
                    var houses = DataAccess.Warehouse.GetWarehouses().ToDictionary(p => p.ID);

                    List<DataWrapper> datasource = new List<DataWrapper>();
                    if (type != "4" && type != "5")
                    {
                        datasource.AddRange(DataAccess.PurchaseBill.Search(dtStart.Date, dtEnd.Date.AddDays(1), int.Parse(supplier), int.Parse(type), serialNum, summary).Select(p => new DataWrapper()
                        {
                            ID = p.ID,
                            Date = p.PurchaseDate,
                            SerialNumber = p.SerialNumber,
                            Status = p.Status,
                            Summary = p.Summary,
                            SupplierName = suppliers[p.SupplierID].Name,
                            HouseName = houses[p.HouseID].Name,
                            TypeName = GetTypeName(p.BillType),
                            Type = p.Type,
                            CreateDate = p.CreateDate
                        }).ToArray());
                    }

                    Expression<Func<DataAccess.PaymentBill, bool>> predicate = null;
                    if (type == "-1")
                    {
                        predicate = p => (supplier == "-1" || p.SupplierID == int.Parse(supplier)) && (serialNum == "" || p.SerialNumber == serialNum) && (summary == "" || p.Summary.Contains(summary)) && p.ReceiptDate >= dtStart.Date && p.ReceiptDate < dtEnd.Date.AddDays(1) && !p.PruchageBillID.HasValue;
                        
                    }
                    else if (type == "4")
                    {
                        predicate = p => !p.IsIncome && (supplier == "-1" || p.SupplierID == int.Parse(supplier)) && (serialNum == "" || p.SerialNumber == serialNum) && (summary == "" || p.Summary.Contains(summary)) && p.ReceiptDate >= dtStart.Date && p.ReceiptDate < dtEnd.Date.AddDays(1) && !p.PruchageBillID.HasValue;
                    }
                    else if (type == "5")
                    {
                        predicate = p => p.IsIncome && (supplier == "-1" || p.SupplierID == int.Parse(supplier)) && (serialNum == "" || p.SerialNumber == serialNum) && (summary == "" || p.Summary.Contains(summary)) && p.ReceiptDate >= dtStart.Date && p.ReceiptDate < dtEnd.Date.AddDays(1) && !p.PruchageBillID.HasValue;
                    }
                    if (predicate != null)
                    {
                        datasource.AddRange(DataAccess.PaymentBill.GetList(predicate).Select(p => new DataWrapper()
                        {
                            ID = p.ID,
                            Date = p.ReceiptDate,
                            SerialNumber = p.SerialNumber,
                            Status = p.Status,
                            Summary = p.Summary,
                            SupplierName = suppliers[p.SupplierID].Name,
                            HouseName = string.Empty,
                            TypeName = p.IsIncome ? "收款单" : "付款单",
                            Type = p.IsIncome ? -2 : -1,
                            CreateDate = p.CreateDate
                        }).ToArray());
                    }


                    this.rptBills.DataSource = datasource.OrderBy(p => p.CreateDate).OrderBy(p => p.Date);
                    this.rptBills.DataBind();
                }
                else
                {
                    this.tbStartDate.Text = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd"); ;
                    this.tbEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
            }
        }

        class DataWrapper
        {
            public Guid ID { get; set; }
            public DateTime Date { get; set; }
            public string SerialNumber { get; set; }
            public string SupplierName { get; set; }
            public int Status { get; set; }
            public string Summary { get; set; }
            public string HouseName { get; set; }
            public string TypeName { get; set; }
            public int Type { get; set; }
            public DateTime CreateDate { get; set; }
        }

        string GetTypeName(DataAccess.PurchaseBillType tp)
        {
            switch (tp)
            {
                case Warehouse.DataAccess.PurchaseBillType.Return:
                    return "退货单";
                case Warehouse.DataAccess.PurchaseBillType.Purchase:
                    return "进货单";
                case Warehouse.DataAccess.PurchaseBillType.Gift:
                    return "赠品单";
                default:
                    return string.Empty;
            }
        }
    }
}
